package com.czd.support.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czd.base.model.Paginate;
import com.czd.base.utils.WebUtils;
import com.czd.support.common.Configuration;
import com.czd.support.model.Company;
import com.czd.support.model.Introduction;
import com.czd.support.model.Manager;
import com.czd.support.model.Navigate;
import com.czd.support.service.CompanyService;
import com.czd.support.service.IntroductionService;
import com.czd.support.service.NavigateService;

/**
 * @Description 企业详情介绍控制类
 * @Author CaoRen
 * @Date 2015/06/18
 */
@Controller
@RequestMapping(value = "/introduction")
public class IntroductionController extends BaseController{
	@Autowired
	private IntroductionService introductionService;
	@Autowired
	private CompanyService companyService;
	@Autowired
	private NavigateService navigateService;
	
	/**
	 * 获取所有的企业
	 * @return
	 */
	@ModelAttribute("companies")
	protected List<Company> getAllCompanies(){
		return companyService.getAllCompanies();
	}
	
	/**
	 * 查询企业的所有导航
	 * @param companyId
	 * @return
	 */
	@RequestMapping(value = "/getNavsByCompany")
	@ResponseBody
	protected List<Navigate> getNavsByCompany(int companyId){
		return navigateService.findByCompanyId(companyId);
	}
	/**
	 * 查询企业详情介绍列表
	 * @param introduction
	 * @param request
	 * @param model
	 * @param pageNo
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list(Introduction introduction,HttpServletRequest request, Model model, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo){
		String kw = WebUtils.findParamStr(request, "kw");
		introduction.setPageSize(Configuration.getInstance().getDefault_page_size());
		introduction.setPageNo(pageNo);
		Paginate paginate = introductionService.queryForPage(introduction, kw);
		model.addAttribute("paginate", paginate);
		return "introduction/list";
	}
	
	/**
	 * 添加页面跳转
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(){
		return "introduction/add";
	}
	
	/**
	 * 添加企业详情介绍
	 * @param introduction
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addIntroduction(Introduction introduction, HttpServletRequest request, Model model){
		Company company = companyService.findById(introduction.getCompanyId());
		Navigate navigate = navigateService.findByComNav(introduction.getNavigateId(),introduction.getCompanyId());
		Manager manager = getSesionUser(request);
		introduction.setCreateUser(manager.getUserCode());
		if(introductionService.insert(introduction)){
			model.addAttribute("introduction", introduction);
			model.addAttribute("company", company);
			model.addAttribute("navigate", navigate);
			return "redirect:/introduction/"+introduction.getId()+"/details";
		}
		return error(model, "提示信息", "添加失败");
	}
	
	/**
	 * 编辑页面跳转
	 * @return
	 */
	@SuppressWarnings("unused")
	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	public String edit(@PathVariable int id,Model model){
		Introduction introduction = introductionService.findById(id);
		Navigate navigate = navigateService.findByComNav(introduction.getNavigateId(),introduction.getCompanyId());
		if(introduction != null){
			model.addAttribute("introduction", introduction);
			model.addAttribute("navigate", navigate);
			return "introduction/edit";
		}
		return error(model, "提示信息", "详情介绍不存在");
	}
	
	/**
	 * 编辑企业详情介绍
	 * @return
	 */
	@RequestMapping(value = "/editIntroduction", method = RequestMethod.POST)
	public String editIntroduction(Introduction introduction,Model model){
		Company company = companyService.findById(introduction.getCompanyId());
		Navigate navigate = navigateService.findByComNav(introduction.getNavigateId(),introduction.getCompanyId());
		if(introductionService.update(introduction)){
			model.addAttribute("company", company);
			model.addAttribute("navigate", navigate);
			model.addAttribute("introduction", introduction);
			return "redirect:/introduction/"+introduction.getId()+"/details";
		}
		return error(model, "提示信息", "修改失败");
	}
	
	/**
	 * 删除企业详情介绍
	 * @return
	 */
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String delete(@PathVariable int id,Model model){
		if(introductionService.delete(id)){
			return "redirect:introduction/list";
		}
		return error(model, "提示信息", "删除失败");
	}
	
	/**
	 * 企业详情介绍
	 * @return
	 */
	@SuppressWarnings("unused")
	@RequestMapping(value = "/{id}/details", method = RequestMethod.GET)
	public String details(@PathVariable int id,Model model){
		Introduction introduction = introductionService.findById(id);
		Company company = companyService.findById(introduction.getCompanyId());
		Navigate navigate = navigateService.findByComNav(introduction.getNavigateId(),introduction.getCompanyId());
		if(introduction != null){
			model.addAttribute("company", company);
			model.addAttribute("navigate", navigate);
			model.addAttribute("introduction", introduction);
			return "introduction/details";
		}
		return error(model, "提示信息", "企业详情介绍不存在");
	}
}
